package com.wzq.app.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.afollestad.materialdialogs.MaterialDialog;

import org.simple.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;


/**
 * @author:ruchen 功能描述:
 * 创建日期:2017/3/20
 * 修改者，修改日期，修改内容。
 */
public abstract class BaseActivity extends AppCompatActivity{
    public Context mContext;
    public WeakReference weakReference;
    private List<Call> calls = new ArrayList<>();
    private MaterialDialog materialDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mContext = this;
        if(isUseEventBus()){
            EventBus.getDefault().register(this);
        }
        weakReference = new WeakReference(this);
        ButterKnife.bind(this);
        initData();
        configView();
        requestData();
    }
    public void showProgressDialog(){
        showProgressDialog("加载中...");
    }
    public void showProgressDialog(String content){
        if(materialDialog == null){
            materialDialog = new MaterialDialog.Builder(this)
                    .content(content)
                    .cancelable(false)
                    .progress(true, 0)
                    .show();
        }
    }

    public void dimissProgressDialog(){
        if(materialDialog!=null&&materialDialog.isShowing()){
            materialDialog.dismiss();
            materialDialog = null;
        }
    }

    protected  boolean isUseEventBus(){
        return false;
    }

    public void initData() {
    }

    public abstract int getLayoutId();
    public abstract void configView();
    public abstract void requestData();

    @Override
    protected void onDestroy() {
        if(isUseEventBus()){
            EventBus.getDefault().unregister(this);
        }
        if(materialDialog!=null&&materialDialog.isShowing()){
            materialDialog.dismiss();
            materialDialog = null;
        }
        cancel();
        super.onDestroy();
    }

    private void cancel(){
        for (Call call : calls) {
            if(call!=null&&!call.isCanceled()) {
                call.cancel();
            }
        }
    }

    public void putCall(Call... datas){
        for (Call call : datas) {
            if(call!=null&&!calls.contains(call)) {
                calls.add(call);
            }
        }
    }


//    public void remind() {
//        Call<BaseModel<DeviceInfo>> call = Api.getInstance().service.arriveByOff(mBusInfo.lineId, udType
//                , mBusInfo.sites.get(mStationAdapter.getmSelectIndex()).stationPointId);
//        putCall(call);
//        call.enqueue(new Callback<BaseModel<DeviceInfo>>() {
//            @Override
//            public void onResponse(Call<BaseModel<DeviceInfo>> call, retrofit2.Response<BaseModel<DeviceInfo>> response) {
//                BaseModel<DeviceInfo> baseModel = response.body();
//                if (response.isSuccessful() && baseModel != null) {
//                    if (TextUtils.equals(baseModel.responseCode, ApiResponseCode.RESPHONE_OK)) {
//                        showRemindDialog(baseModel.responseData.deviceId, baseModel.responseData.deviceName);
//                    } else if (TextUtils.equals(baseModel.responseCode, ApiResponseCode.NO_DATA)) {
//                        ToastUtils.showToast(mContext, "没有即将到站的公交！");
//                    } else if (baseModel.responseCode.equals(ApiResponseCode.NO_LOGIN)) {
//                        LaunchUtils.launchLoginGoMain(mContext);
//                    } else {
//                        ToastUtils.showToast(mContext, baseModel.responseMessage);
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseModel<DeviceInfo>> call, Throwable t) {
//                ToastUtils.showToast(mContext, t.getMessage());
//            }
//        });
//    }

}
